#ifndef _SETTING_H_
#define _SETTING_H_

#include <thread>
#include <mutex>
#include <string>
#include <string.h>

#include "SettingUI.h"

using namespace std;

class Setting;

class Setting
{
private:
    struct SysConfigMem // 用于共享内存的系统配置
    {
        int magic;
        int brightness;
        int volume;
        char mainbgFile[32];
        char weatherKey[32];
        
        int appNum;
        struct AppInfoMem
        {
            char name[32];
            char config[32];
        } appInfo[0];
    };

    static constexpr int shmKey = 114486;

private:
    int shmId;
    string connectedSSID; // 已连接的wifi ssid
    std::thread *pThread; // 数据处理线程
    mutex *uiMutex;
    bool threadExitFlag;  // 指定线程退出标记

    SysConfigMem *config = nullptr;

    string &updateConnection(void);
    bool getNetInfo(void);
    void *getShareMem(void);

    int threadFunction(void);
    bool wlanScanThreadFunction(void);
    void wlanConnectFunction(int id, const char *_ssid, const char *_psk);

    void poweroff(void);
    void reboot(void);
    void wlanScan(void);
    int wlanSearch(string &ssid, string &psk);
    void wlanConnect(int id, const char *_ssid, const char *_psk);
    void wlanChangePassword(int id, const char *psk);
    void wlanDisconnect(int id);
    int wlanAddNetwork(string &ssid, string &psk);
    void getConfig(void);
    bool saveConfig(int pageIndex, vector<SettingUI::ConfigItemInfo> & itemVector);

public:
    Setting(function<void(void)>, mutex &UIMutex);
    ~Setting();
};

#endif